import { defineStore } from 'pinia';

type State = {
  cachedViews: string[]
}

export const useKeepAliveStore = defineStore('_keepAlive', {
  state: (): State => ({
    cachedViews: [],
  }),
  actions: {
    addCacheOnly(route: TypeObject) {
      const name = route.name;
      if (!this.cachedViews.includes(name)) {
        this.cachedViews.push(name);
      }
    },
    deleteCacheOnly(route: TypeObject) {
      const name = route.name;
      const index = this.cachedViews.indexOf(name);
      if (index > -1) {
        this.cachedViews.splice(index, 1);
      }
    },
  },
});
